package com.onemile.app;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ImageView;
import com.onemile.app.base.BaseUINoTop;
import com.onemile.controls.AdpCst_Gallery3D;
import com.onemile.controls.CstGallery3D;
import com.onemile.controls.CstPanel;
import com.onemile.controls.CstPanel.OnPanelListener;

import com.onemile.entity.PubConst;
import com.onemile.entity.Session;
import com.onemile.interpolator.EasingType.Type;
import com.onemile.interpolator.ElasticInterpolator;
import com.onemile.utils.UtiDialogsFactory;
import com.onemile.utils.UtiUI;

public class UIHome extends BaseUINoTop
{
   private CstPanel cstPanel;
   private ImageView imgPanelMenu;

   private CstGallery3D gallery3D;

   private ImageView imgRepast;
   private ImageView imgDrink;
   private ImageView imgSmart;
   private ImageView imgAccount;

   private ImageView imgLogin;
   private ImageView imgRegister;

   private void InitControls()
   {
	  cstPanel = (CstPanel) findViewById(R.id.topMenu_home);
	  imgPanelMenu = (ImageView) findViewById(R.id.img_in_panel_menu);
	  gallery3D = (CstGallery3D) findViewById(R.id.cstGallery3D_home);

	  imgRepast = (ImageView) findViewById(R.id.imgRepast_home_menu);
	  imgDrink = (ImageView) findViewById(R.id.imgDrink_home_menu);
	  imgSmart = (ImageView) findViewById(R.id.imgSmart_home_menu);
	  imgAccount = (ImageView) findViewById(R.id.imgAccount_home_menu);

	  imgLogin = (ImageView) findViewById(R.id.imgLogin_home);
	  imgRegister = (ImageView) findViewById(R.id.imgRegister_home);
   }

   private void InitControlsEvent()
   {
	  imgRepast.setOnClickListener(new View.OnClickListener()// 餐饮
			{
			   @Override
			   public void onClick(View v)
			   {
				  Intent intent = new Intent();
				  intent.setClass(UIHome.this, UIShopList.class);
				  intent.putExtra(PubConst.Intent_Key.SHOP_TYPE, 1);
				  startActivity(intent);
			   }
			});
	  imgDrink.setOnClickListener(new View.OnClickListener()// 饮料
			{
			   @Override
			   public void onClick(View v)
			   {
				  Intent intent = new Intent();
				  intent.setClass(UIHome.this, UIShopList.class);
				  intent.putExtra(PubConst.Intent_Key.SHOP_TYPE, 2);
				  startActivity(intent);
			   }
			});
	  imgSmart.setOnClickListener(new View.OnClickListener()// 便利店
			{
			   @Override
			   public void onClick(View v)
			   {
				  Intent intent = new Intent();
				  intent.setClass(UIHome.this, UIShopList.class);
				  intent.putExtra(PubConst.Intent_Key.SHOP_TYPE, 4);
				  startActivity(intent);
			   }
			});
	  imgAccount.setOnClickListener(new View.OnClickListener()// 我的账户
			{
			   @Override
			   public void onClick(View v)
			   {
				  if (Session.getInstance().IsLogin())
				  {
					 Intent intent = new Intent();
					 intent.setClass(UIHome.this, UIAccount.class);
					 intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
					 startActivity(intent);
				  }
				  else
				  {
                     Intent intent=new Intent();
                     intent.setClass(UIHome.this, UILogin.class);
                     intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                     startActivity(intent);
				  }
			   }
			});

	  imgLogin.setOnClickListener(new View.OnClickListener()// 登录
			{
			   @Override
			   public void onClick(View v)
			   {
				  if (Session.getInstance().IsLogin())
				  {
					 UtiDialogsFactory.getInstance().ShowToastInfoShort(UIHome.this, getString(R.string.login_already_login));
				  }
				  else
				  {
					 Intent intent = new Intent();
					 intent.setClass(UIHome.this, UILogin.class);
					 startActivityForResult(intent, PubConst.ActivityCode.LOGIN);
				  }
			   }
			});
	  imgRegister.setOnClickListener(new View.OnClickListener()// 注册
			{
			   @Override
			   public void onClick(View v)
			   {
				  Intent intent = new Intent();
				  intent.setClass(UIHome.this, UIRegister.class);
				  startActivityForResult(intent, PubConst.ActivityCode.REGISTER);
			   }
			});
   }

  
   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event)
   {
	  if (keyCode == event.KEYCODE_BACK)
	  {
		 UtiDialogsFactory.getInstance().ShowOKCancelAlertDialog(UIHome.this, getString(R.string.dialog_tips), getString(R.string.dialog_are_you_sure_exit), new DialogInterface.OnClickListener()
		 {
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
			   android.os.Process.killProcess(android.os.Process.myPid());
			   finish();
			}
		 });
	  }
	  return false;
   }

   @Override
   protected void onCreateOverride(Bundle savedInstanceState)
   {
	  try	  
	  {
		 if (UtiUI.getInstance() == null)
		 {
			UtiUI utiUI = new UtiUI(this);
		 }
		 InitControls();
		 InitControlsEvent();

		 Integer[] images = { R.drawable.home_smart, R.drawable.home_repast, R.drawable.home_drink };
		 AdpCst_Gallery3D adapter = new AdpCst_Gallery3D(this, images);
		 adapter.createReflectedImages();// 创建倒影效果

		 gallery3D.setFadingEdgeLength(0);
		 gallery3D.setSpacing(-20); // 图片之间的间距
		 gallery3D.setAdapter(adapter);
		 gallery3D.setOnItemClickListener(new AdapterView.OnItemClickListener()
		 {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
			   Intent intent = new Intent();
			   intent.setClass(UIHome.this, UIShopList.class);
			   switch (position)
			   {
				  case 0:// 便利店
					 intent.putExtra(PubConst.Intent_Key.SHOP_TYPE, 4);
					 break;
				  case 1:// 餐馆
					 intent.putExtra(PubConst.Intent_Key.SHOP_TYPE, 1);
					 break;
				  case 2:// 饮料
					 intent.putExtra(PubConst.Intent_Key.SHOP_TYPE, 2);
					 break;
			   }

			   startActivity(intent);
			}

		 });
		 gallery3D.setSelection(1);

		 imgPanelMenu = (ImageView) findViewById(R.id.img_in_panel_menu);
		 imgPanelMenu.setOnClickListener(new View.OnClickListener()
		 {
			@Override
			public void onClick(View v)
			{
			   cstPanel.setOpen(!cstPanel.isOpen(), true);
			}
		 });

		 OnPanelListener panelListener = new OnPanelListener()
		 {
			@Override
			public void onPanelOpened(CstPanel cstPanel)
			{
			   // TODO 标题栏打开事件

			}

			@Override
			public void onPanelClosed(CstPanel cstPanel)
			{
			   // TODO 标题栏关闭事件

			}
		 };

		 cstPanel.setOnPanelListener(panelListener);
		 cstPanel.setInterpolator(new ElasticInterpolator(Type.IN, 1.0f, 0.3f));
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "UIHome_onCreate(savedInstanceState) ", e);
		 }
	  }
	  
   }

   @Override
   public int SetContentViewLayoutResId()
   {
	  return R.layout.home;
   }


}
